package com.yueyang.sojourn.server.service.sojourn.merchant;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.model.enums.StatusEnum;
import com.yueyang.sojourn.server.model.po.SojournMerchant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户服务
 * @author andy.yang
 * @version v1
 * @summary
 * @Copyright (c) 2019, yueyang Group All Rights Reserved.
 * @since 2020/3/17
 */
@Service
public class SojournMerchantService {
    @Resource
    SojournMerchantMapper sojournMerchantMapper;

    /**
     * 创建商户
     * @param merchant
     * @return
     */
    public Long add(SojournMerchant merchant){
        merchant.setStatus(StatusEnum.VALID.getCode());
        sojournMerchantMapper.insert(merchant);
        return merchant.getId();
    }

    /**
     * 编辑商户
     * @param merchant
     * @return
     */
    public int update(SojournMerchant merchant){
        return sojournMerchantMapper.update(merchant);
    }

    /**
     * 删除
     * @param merchantId
     * @param userId
     * @return
     */
    public int delete(Long merchantId,Long userId){
        return sojournMerchantMapper.delete(merchantId,userId);
    }
    /**
     * @param keyword      关键字
     * @param pageNo      页码
     * @param pageSize    页大小
     * @return com.github.pagehelper.PageInfo<com.yueyang.sojourn.server.model.vo.SojournMerchVo>
     * @author Andy.yang
     * @summary 订单分页查询
     * @version v1
     * @since 2020-02-19
     */
    public PageInfo<SojournMerchant> findPage(String keyword, Integer status,int pageNo, int pageSize) {
        PageInfo<SojournMerchant> pageInfo = PageHelper.startPage(pageNo, pageSize)
                .doSelectPageInfo(() -> sojournMerchantMapper.findBy(keyword,status));
        return pageInfo;
    }

    /**
     * @param merchantId      商户id
     * @return com.github.pagehelper.PageInfo<com.yueyang.sojourn.server.model.vo.SojournMerchVo>
     * @author Andy.yang
     * @summary 查询
     * @version v1
     * @since 2020-02-19
     */
    public SojournMerchant findById(Long merchantId) {
        return sojournMerchantMapper.selectByPrimaryKey(merchantId);
    }


    /**
     * @param name      商户名
     * @return com.github.pagehelper.PageInfo<com.yueyang.sojourn.server.model.vo.SojournMerchVo>
     * @author Andy.yang
     * @summary 查询
     * @version v1
     * @since 2020-02-19
     */
    public SojournMerchant findByName(String name) {
        return sojournMerchantMapper.findByName(name);
    }


    /**
     * @param wxMerchantNo      微信商户号
     * @return com.github.pagehelper.PageInfo<com.yueyang.sojourn.server.model.vo.SojournMerchVo>
     * @author Andy.yang
     * @summary 查询
     * @version v1
     * @since 2020-02-19
     */
    public SojournMerchant findByWxMerchantNo(String wxMerchantNo) {
        return sojournMerchantMapper.findByWxMerchantNo(wxMerchantNo);
    }

    /**
     * 根据项目id查询项目的主商户（拥有者）
     * @return
     */
    public SojournMerchant findByProjectId(Long projectId){
        return sojournMerchantMapper.findByProjectId(projectId);
    }

    /**
     * 根据ids查询商户
     * @param ids
     * @return
     */
    public List<SojournMerchant> findByIds(List<Long> ids){
        return sojournMerchantMapper.findByIds(ids);
    }
    /**
     * 根据ids查询商户
     * @param ids
     * @return
     */
    public Map<Long,SojournMerchant> findMapByIds(List<Long> ids){
        Map<Long,SojournMerchant> merchantMap = new LinkedHashMap<>();
        List<SojournMerchant> merchants = sojournMerchantMapper.findByIds(ids);
        if(merchants != null){
            merchants.stream().forEach((m) -> {merchantMap.put(m.getId(),m);});
        }
        return merchantMap;
    }
}
